<?php
define('APPGALLERY','1.00');
/*
 * ImageGallery
 * Copyright (C) 2006 Daniele Deplano
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the license, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 */

/*
 * Blocco per VisualFrameWork (http://www.VisualFrameWork.it) di Roberto Viola, Daniele Deplano, Stefano Toxiri
 * che permette di creare automaticamente una galleria di immagini a partire da
 * quelle che ci sono nella directory dove e' presente questo file.
 *
 *
 * Blocco Liberamente tratto da FlatGallery v. 0.3 di Marco Segato
 *

 * Autore    Daniele Deplano  <nost@tiscali.it>
 * Website   http://www.nost.it
 * Versione  0.1
 * Data      18/02/2006
 */
?>

<style>

table#gallery
{
 width: 100%;
border-collapse: collapse;
}

table#gallery tr.firstrow
{
background: #EEE;
text-transform: uppercase;
}

table#gallery td
{
width: 33%;
text-align: center;
font-size: 11px;
border: 1px solid #CCC;
}
</style>

<?php structure_openBlock("table appl","Gallery",""); ?>

	<p>


<?php

// directory dove sono presenti le immagini
$pic_path = "datas/applications/".$apptitle;

if(!file_exists("datas/applications/"))mkdir("datas/applications/");
if(!file_exists("datas/applications/".$apptitle))mkdir("datas/applications/".$apptitle);


if ((defined('LIBGROUP') && group_getusergroup("multigallery",user_getuser())) || admin_isadmin()){
    ?>
    <hr />
    <form name="gallery_upload" action="index.php?appname=<?=$appname?>&op=upload" enctype='multipart/form-data' method='post'>
    carica un immagine<input type="file" name="image"><input type=submit value="load">
    </form>
    <hr />
    <?php
	$op=$_GET['op'];
	if ($op=="upload"){
	    admin_writelog("Gallery:upload");
	    if (move_uploaded_file($_FILES['image']['tmp_name'], $pic_path."/".$_FILES['image']['name'])) {
		//echo $_FILES['image']['name'];
		if (strtolower(substr($_FILES['image']['name'],-4))==".php" )
		    unlink($pic_path."/".$_FILES['image']['name']);
		else{
		    echo "<script>alert('immagine ".$_FILES['image']['name']." caricata');</script>";
		}
	    }
	}
	if ($op=="delimage"){
	    admin_writelog("Gallery:delimage");

	    $image=$_GET['image'];
	    if (file_exists($pic_path."/".$image)){
		unlink($pic_path."/".$image);
		echo "<script>alert('immagine $image eliminata');</script>";
	    }
	}
	if ($op=="renameimage"){

	    $image=$_GET['image'];
	    if (file_exists($pic_path."/".$image)){
		echo"<hr />
		    <form name='gallery_rename' action='index.php?appname=$appname&op=exec_rename' method='post'>
		    <input type='hidden' name='image' value='$image'>
		    nuovo nome<input type='text' name='newname' value='$image'><input type=submit value='rename'>
		    </form>
		    <hr />
		";
	    }
	}
	if ($op=="exec_rename"){
	    admin_writelog("Gallery:renameimage");
	    $image=$_POST['image'];
	    $newname=$_POST['newname'];
	    rename($pic_path."/".$image,$pic_path."/".$newname);
	}
    }

// carico tutti i files immagine in un array
$dir = opendir($pic_path);

$n_file = 0;
while ($filename = readdir($dir)){
    //echo substr($filename,-4);
	if(  substr($filename,-4)==".gif" || substr($filename,-4)==".jpg" || substr($filename,-5)==".jpeg" || substr($filename,-4)==".png" || substr($filename,-4)==".GIF" || substr($filename,-4)==".JPG" || substr($filename,-4)==".JPEG" ||substr($filename,-4)==".PNG"){
	    $array_dir[$n_file] = $filename;
	    $n_file++;
	}
}


// ordinamento alfabetico dell'array
if (count($array_dir)){
    sort($array_dir);
    $array_dir = array_values($array_dir);
}
?>




<!-- menu_top select per cambio pagina -->
<div align="center">
	<form name="pagine_top" action=""><?=$txt_pagina?>
	<select name="pag_top" onchange="self.location=document.pagine_top.pag_top[document.pagine_top.pag_top.selectedIndex].value">
		<? for($i=0;$i<$n_file/9;$i++)
			{ if(!isset($_GET['pag']))
				echo "<option value=\"index.php?appname=".$apptitle.$_GET['mod']."&amp;pag=".($i+1)."\">".($i+1)."</option>";
			else if($_GET['pag'] == $i+1)
				echo "<option value=\"index.php?appname=".$apptitle.$_GET['mod']."&amp;pag=".($i+1)."\" selected>".($i+1)."</option>";
				else echo "<option value=\"index.php?appname=".$apptitle.$_GET['mod']."&amp;pag=".($i+1)."\">".($i+1)."</option>";
			}
		?>
	</select>
	</form>
</div>

<!-- tabella 9x9 thumbnails -->
<?php if(count($array_dir)){ ?>
<table id="gallery"><tbody>
	<?php if(isset($_GET['pag']))				// determina qual e' la prima foto della pagina: 1,10,19...
		$max_x_pag = ($_GET['pag'] * 9) - 8;
	else $max_x_pag = 1;

	for ($i=$max_x_pag; $i<$max_x_pag+9 and $i<=$n_file; $i++){
	    if($i%3 == 1)					// apertura nuova riga prima delle immagini numero 1,4,7...
		echo "<tr >";

		list($width, $height) = getimagesize($pic_path."/".$array_dir[$i-1]);
		// dimensione massima finestre con immagini: 800x600
		if($width>1000 OR $height>650){
			$width_max = 800;
			$height_max = 600;
			$scrollbars = "yes";
		}
		else {
			$width_max = $width;
			$height_max = $height;
			$scrollbars = "no";
		}
		?>
		<td >	<!-- stampa casella con immagine -->
		    <?php $my_window="MyWindow=window.open('$pic_path/".$array_dir[$i-1]."', 'MyWindow', 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=$scrollbars, resizable=no, width=$width_max, height=$height_max, left=10, top=10');" ?>
		  <a href="#" onClick="<?=$my_window ?>"><img border="0" src="<?="$pic_path/".$array_dir[$i-1]?>" width='160' alt="thumb"></a>
		  <br><b><?=$array_dir[$i-1]?></b>
		  <br><?=round(filesize($pic_path."/".$array_dir[$i-1]) / 1024, 1)." Kb, $width"."x$height"?>
		  <?php  if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){ ?>
		     | <a href="index.php?appname=<?=$appname?>&op=delimage&image=<?=$array_dir[$i-1]?>">delete</a>
		     | <a href="index.php?appname=<?=$appname?>&op=renameimage&image=<?=$array_dir[$i-1]?>">rename</a>
		  <?php } ?>

		</td>
		<?
		if($i%3 == 0)					// chiusura della riga dopo le immagini numero 3,6,9...
			echo "</tr>";
		}
	while (($i-1)%3 != 0)					// stampa casella vuota per terminare correttamente la riga
	    { echo "<td width=\"225\" height=\"225\"></td>";
		$i++;
		}
	?>
</tbody></table><br>
<?php } ?>

<!-- menu_bottom select per cambio pagina -->
<div align="center">
	<form name="pagine_bottom" action=""><?=$txt_pagina?>
	<select name="pag_bottom" onchange="self.location=document.pagine_bottom.pag_bottom[document.pagine_bottom.pag_bottom.selectedIndex].value">
		<? for($i=0;$i<$n_file/9;$i++)
			{ if(!isset($_GET['pag']))
				echo "<option value=\"index.php?appname=".$apptitle.$_GET['mod']."&amp;pag=".($i+1)."\">".($i+1)."</option>";
			else if($_GET['pag'] == $i+1)
				echo "<option value=\"index.php?appname=".$apptitle.$_GET['mod']."&amp;pag=".($i+1)."\" selected>".($i+1)."</option>";
				else echo "<option value=\"index.php?appname=".$apptitle.$_GET['mod']."&amp;pag=".($i+1)."\">".($i+1)."</option>";
			}
		?>
	</select>
	</form>
	<!-- footer -->
	<br><hr size="1" width="98%">
	Powered by <a href="http://www.nost.it" target="_blank">Image_Multi_Gallery</a> 0.1<br>Copyright &#169; 2007 Daniele Deplano
</div>


	</p>

<?php structure_closeBlock(); ?>


